/**
 * $Id$
 *
 * Copyright (c) 2014 ChinaSoft International, Ltd. All rights reserved
 * JointForce Project
 *
 */
package com.csi.jf.crucible.api.crowd;

import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.chinasofti.ro.bizframework.modules.json.JSONObject;
import com.csi.jf.crucible.api.crucible.RestClient;
import com.icss.j2ee.util.http.HttpClientException;

/**
 * 
 * @Title: ApiTestCrowdUsers.java
 * @Description: <br>
 *               <br>
 * @Company: ICSS
 * @Created on 2014-7-9 下午3:41:27
 * @author FengHaiBing
 */
public class ApiTestCrowdUsers {
    
	private final String CROWD_HOST = "http://115.29.76.124:8095";
    private RestClient client = new RestClient();
    
    public static void main(String[] args) throws Exception{
        ApiTestCrowdUsers test = new ApiTestCrowdUsers();
        test.getLoginToken("demo","123456");
        
//        String userName = "f_test", firstName = "feng", lastName = "haibing", displayName = "fhb Test", email = "test@chinasofti.com", password="123456" ;
//        test.createUser(userName, firstName, lastName, displayName, email, password);
    }
    
    /**
     * @param string username
     * @param string password
     * @return
     *{
     *  "token":"zhangmh:2:d249481bd97fc34edc6e855731168517"
     * }
     * @throws Exception
     */
    public void getLoginToken(String username,String password)throws Exception{
        String url = CROWD_HOST + "/crowd/rest/usermanagement/1/authentication?username=" + username;
        String json = "{   \"value\" : \""+password+"\"} ";
//        client.doPost(SONAR_URL, json);
        StringBuffer sbXml = new StringBuffer();
        sbXml
        .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
        .append("<password>")
        .append("    <value>"+password+"</value>")
        .append("</password>")
        ;
        doPostWithXml(url, sbXml.toString(), false);
    }
    
    public String doPostWithXml(String url,String xml,boolean useToken) throws HttpClientException, IOException{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        StringEntity myEntity = new StringEntity(xml, "GBK");
        httppost.addHeader("Content-Type", "text/xml");
        httppost.setEntity(myEntity);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
        InputStreamReader reader = new InputStreamReader(resEntity.getContent(), "ISO-8859-1");
        char[] buff = new char[1024];
        int length = 0;
        while ((length = reader.read(buff)) != -1) {
                System.out.println(new String(buff, 0, length));
        }
        httpclient.getConnectionManager().shutdown(); 
        
        return "";
    }
    
    public void createUser(String userName, String firstName, String lastName,
    		String displayName, String email, String password) throws HttpClientException, IOException {
    	JSONObject jo = new JSONObject();
    	JSONObject joValue = new JSONObject();
    	jo.put("name", userName);
    	jo.put("first-name", firstName);
    	jo.put("last-name", lastName);
    	jo.put("display-name", displayName);
    	jo.put("email", email);
    	joValue.put("value", password);
    	jo.put("password", joValue);
    	jo.put("active", Boolean.TRUE);
    	
        String url = CROWD_HOST + "/crowd/rest/usermanagement/1/user";
        client.doPost(url, jo.toString());
//    	StringBuffer sbJson = new StringBuffer();
//    	sbJson
//    	.append("{   \"name\" : \""+userName+"\",")
//    	.append("   \"first-name\" : \""+firstName+"\",")
//    	.append("   \"last-name\" : \"Username\",")
//    	.append("   \"display-name\" : \"My Username\",   \"email\" : \"user@example.test\",   \"password\" : {      \"value\" : \"my_password\"   },   \"active\" : true} ")
//    	.append("")
//    	.append("")
//    	.append("")
//    	.append("")
//    	.append("")
//    	;
    }
    
    /**
     * 
     * 
     * @Created on 2014-7-9 下午7:24:58
     * @author FengHaiBing
     * @throws IOException 
     * @throws HttpClientException 
     */
    public void listAllUsers() throws HttpClientException, IOException {
    	String url = CROWD_HOST + "/crowd/rest/usermanagement/latest/search?entity-type=user";
      client.doGet(url);
    }

}
